# Definition for a binary tree node.
from collections import Counter
from typing import Optional, List


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def findFrequentTreeSum(self, root: Optional[TreeNode]) -> List[int]:
        sum_list = []

        def dfs(root: Optional[TreeNode]) -> int:
            nonlocal sum_list

            if root is None:
                return 0
            if root.left is None and root.right is None:
                sum_list.append(root.val)
                return root.val
            tree_sum = root.val + dfs(root.left) + dfs(root.right)
            sum_list.append(tree_sum)
            return tree_sum

        dfs(root)
        counter = Counter(sum_list)
        return [k for k, v in counter.items() if v == max(counter.values())]
